package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 分类品牌Service业务层处理
 */
@Service
@Transactional
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand>
        implements ICategoryBrandService {
    @Autowired
    CategoryBrandMapper categoryBrandMapper;
    @Autowired
    ICategoryService categoryService;

    /**
     * 查询分类品牌列表
     * @param categoryBrand 实体类
     * @return TableDataInfo(list)
     */
    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryBrand) {
        return categoryBrandMapper.selectCategoryBrandList(categoryBrand);
    }

    /**
     * 获取分类品牌详细信息
     * @param id 商品单位主键
     * @return 商品单位
     */
    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = categoryBrandMapper.selectById(id);
        List<Long> categoryIdList = categoryService.getAllCategoryIdList(categoryBrand.getCategoryId());
        categoryBrand.setCategoryIdList(categoryIdList);
        return categoryBrand;
    }

    /**
     * 新增分类品牌
     * @param categoryBrand categoryBrand
     * @return 返回信息
     */
    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        Long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>()
                .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
        if(count>0){
            throw new ServiceException("该分类已添加该品牌");
        }
        return categoryBrandMapper.insert(categoryBrand);
    }

    /**
     * 修改分类品牌
     * @param categoryBrand categoryBrand
     * @return 返回信息
     */
    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        CategoryBrand categoryBrand1 = categoryBrandMapper.selectById(categoryBrand.getId());
        if(categoryBrand1.getCategoryId().longValue() != categoryBrand.getCategoryId().longValue() ||
            categoryBrand1.getBrandId().longValue() != categoryBrand.getBrandId().longValue())
        {
            Long count = categoryBrandMapper.selectCount(new LambdaQueryWrapper<CategoryBrand>()
                    .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                    .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId()));
            if(count>0){
                throw new ServiceException("该分类已添加该品牌");
            }
        }
        return categoryBrandMapper.updateById(categoryBrand);
    }

    /**
     * 根据分类Id获取品牌列表
     * @param categoryId 分类Id
     * @return 品牌列表
     */
    @Override
    public List<Brand> selectBrandByCategoryId(Long categoryId) {
        return categoryBrandMapper.selectBrandByCategoryId(categoryId);
    }
}
